<template>

		<a-config-provider :theme="myTheme" :locale="zhCN">
			<a-app v-show="!inPending">
				<router-view  v-slot="{Component}">
					<keep-alive :include="cachedViews">
						<component :is="Component"  :key="routeKey"/>
					</keep-alive>
				</router-view>
			</a-app>
			<app-loading v-if="inPending" :loading-text="appName"/>
		</a-config-provider>
</template>

<script setup lang="ts">
import { theme } from 'ant-design-x-vue';
import myColor from '@/utils/myColor'
import zhCN from 'ant-design-vue/es/locale/zh_CN';  //语言国际化（内置的dayjs时间组件除外）
import {useDark} from "@vueuse/core";
const appName:string = import.meta.env.VITE_APP_NAME

const {storeCacheView} = useStore()
const {cachedViews} = storeToRefs(storeCacheView)
const currentRoute=useRoute()
const routeKey =computed(()=>{
	return currentRoute.meta.routerViewKey || currentRoute.path  // 优先赋值为 routerViewKey
})
const {storeUser} = useStore()  //从store工厂中取出要指定使用的若干个状态数据
const isDark = useDark()  //是否暗黑模式？
console.log('isDark',isDark.value)
const myTheme = reactive({
	token: myColor.token,
	algorithm: isDark.value? theme.darkAlgorithm: theme.defaultAlgorithm,
})
provide('myTheme',myTheme)

/* computed */
const inPending = computed(()=>{
	if(currentRoute.path==='/'){
		return true;
	}else if(currentRoute.path==='/login'){
		return false;
	}else  if(!storeUser.id || storeUser.id===''){
		return true;
	}
	return false;
})

const {replacePath} = usePath()

if(currentRoute.path!=='/login'){
//todo: mock无视处理：在用户底座生效之前，生产环境的条件编译处理；未来要把这一段注释掉
	if (import.meta.env.VITE_MODE === 'production') {
		storeUser.id = 'abc-12'
		storeUser.userType = 1
		storeUser.name = '李国庆'
		storeUser.sex = 1
		storeUser.avatar = '/img/avatar/face_1.svg'
		storeUser.mobile = '18655555555'
		localStorage.setItem(import.meta.env.VITE_TOKEN_KEY,'1234-5678-90ab-cdef');
	}
	else
//todo: mock无视处理 END
		storeUser.getUserInfo().then(()=>{
			//console.log(res)
		}).catch(()=>{
			storeUser.resetToken()
			localStorage.removeItem(import.meta.env.VITE_TOKEN_KEY)
			replacePath('/login')
			//message.warning(e.message||'获取用户信息失败')
		})


}

</script>

<style lang="scss">

</style>
